#!/usr/bin/python
import gtk
import pygtk
pygtk.require('2.0')
import gtk.glade
import gobject
import random
import lipsum
import string

gladefile = "practica1iu.glade"

class iugui:
    
 def nombreElementos(self):
     rand = random.randint(0,99)
     return string.join(["Elemento ",str(rand)])
 
 def newElem(self,widget,treeview,statusbar,textview):
    self.menu.deselect()
    id = statusbar.get_context_id("elemento insertado")
    statusbar.pop(id)
    elem = self.nombreElementos()
    listelem = string.split(elem, ' ', 2)
    model = treeview.get_model()
    pos = model.append([elem])
    fila = model.get_path(pos)
    textoContenido = []
    for i in range(0, 10):
        textoContenido += [self.generador.generate_paragraph()]
    textoContenido = [string.join(textoContenido, '\n')]
    textoContenido.append('\n')
    for i in range(0, 10):
        textoContenido += [self.generador.generate_paragraph()]
    textoContenido = [string.join(textoContenido, '\n')]
    textoContenido = string.join(textoContenido, '\n')
    textview.get_buffer().set_text(textoContenido)
    self.cont[int(listelem[2])] = textoContenido
    treeview.set_cursor(fila)
    treeview.grab_focus()
    statusbar.push(id,"elemento insertado")
    return

 def esquema(self,widget,treeview,textview,statusbar):
    id = statusbar.get_context_id("elemento")
    statusbar.pop(id)
    treeselection = treeview.get_selection()
    (model, iter) = treeselection.get_selected()
    nombre = model.get_value(iter,0)
    listelem = string.split(nombre, ' ', 2)
    textview.get_buffer().set_text(self.cont[int(listelem[2])])
    statusbar.push(id,nombre)
    return
    
 def dialog(self,widget):
     self.menu.deselect()
     window = 'aboutdialog1'
     wTree2 = gtk.glade.XML(gladefile,window)
     aboutdialog = wTree2.get_widget(window)
     aboutdialog.set_name('Practica1\nIU')
     aboutdialog.set_transient_for(self.window)
     aboutdialog.set_modal(True)
     aboutdialog.run()
     aboutdialog.destroy()

 def __init__(self):
      
     window = "Practica1"
     
     self.generador = lipsum.generator("sample.txt","dictionary.txt")
     self.wTree = gtk.glade.XML(gladefile,window)
     
     
     self.s = self.wTree.get_widget('scrolledwindow2')
     self.textview = self.wTree.get_widget('textview1')
     self.status = self.wTree.get_widget('statusbar1')
     self.menu = self.wTree.get_widget('Menu')
     self.window = self.wTree.get_widget('Practica1')
     self.m = self.wTree.get_widget('menuitem1')
     self.t = self.wTree.get_widget('toolbutton1')
     self.n = self.wTree.get_widget('nuevo1')
     self.ab = self.wTree.get_widget('acercade1')
     
     self.cont = {}
     self.model = gtk.ListStore(gobject.TYPE_STRING)
     self.treeview = gtk.TreeView(self.model)
     self.treeview.set_headers_visible(True)

     self.renderer=gtk.CellRendererText()
     self.column=gtk.TreeViewColumn("Esquema",self.renderer, text=0)
     self.column.set_resizable(True)
     self.treeview.append_column(self.column)
     
     self.t.connect("clicked",self.newElem,self.treeview,self.status,self.textview)
     
     self.n.connect("activate",self.newElem,self.treeview,self.status,self.textview)
     
     self.ab.connect("activate",self.dialog)
     
     self.treeview.connect("cursor-changed",self.esquema,self.treeview,self.textview,self.status)
     
     self.s.add(self.treeview)
     self.treeview.show()
     
     dic = {   "on_Practica1_destroy"      : gtk.main_quit,
                "on_salir1_activate"       : gtk.main_quit,
                "on_toolbutton2_clicked"   : gtk.main_quit,
                
           }
     
     self.wTree.signal_autoconnect (dic)
     return
 
gui=iugui()
gtk.main()
